package com.zl.web;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SyncServlet", urlPatterns = "/sync")
public class SyncServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        long b = System.currentTimeMillis();
        // 执行业务
        doSomething(response);
        // 打印耗时时间
        System.out.println("sync use: " + (System.currentTimeMillis() - b));
    }

    private void doSomething(HttpServletResponse response) {
        try {
            // 模拟延迟
            Thread.sleep(3000);
            // 模拟业务
            response.getWriter().write("sync, hello !");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
